<?php

declare(strict_types=1);

namespace app\admin\service\dynamic;

use app\api\lib\SuPengJun;
use app\common\model\activity\ActivityModel;
use app\common\model\circle\CircleModel;
use app\common\model\dynamic\DynamicAudioModel;
use app\common\model\dynamic\DynamicCommentModel;
use app\common\model\dynamic\DynamicImgModel;
use app\common\model\dynamic\DynamicLikeModel;
use app\common\model\dynamic\DynamicModel;
use app\common\model\dynamic\DynamicVideoModel;
use app\common\model\order\OrderGoodsModel;
use app\common\model\order\OrderModel;
use app\common\model\system\PluginModel;
use app\common\model\user\UserModel;
use Dengje\Pxy\BaseService;

class CommentService extends BaseService
{
    protected $model = null;


    public function __construct()
    {
        $this->model =  new \app\common\model\dynamic\DynamicCommentModel();
    }

    public function list()
    {
        $where = [];
        $status = request()->get('status', 99);
        $dynamic_id = request()->get('dynamic_id', 0);
        $user_id = request()->get('user_id', 0);
        $content = request()->get('content', '');
        if ($status != 99) $where[] = ['status', '=', $status];
        if ($dynamic_id) $where[] = ['dynamic_id', '=', $dynamic_id];
        if ($user_id) $where[] = ['user_id', '=', $user_id];
        if ($content) $where[] = ['content', 'like', '%' . $content . '%'];
        $list = $this->model->where($where)->with('user')
            ->order(['status' => 'asc', 'id' => 'desc'])
            ->paginate(request()->get('page_size/d', 10));
        $list->map(function ($item) {
            $item['dynamic_content'] = DynamicModel::where('id', $item['dynamic_id'])->value('content');
            $province = $item['province'];
            if (!$province) $province = $item['city'];
            if (!$province) $province = $item['country'];
            $item['ip_str'] = SuPengJun::addsStr($province);
            return $item;
        });
        return $list;
    }

    public function detail($id)
    {
        $detail = $this->model->where('id', $id)->with('user')->find();
        $detail['dynamic_content'] = DynamicModel::where('id', $detail['dynamic_id'])->value('content');
        return $detail;
    }

    public function create($request)
    {
        $data = $this->preExcludeFields($request->param());
        $model = [
            'dynamic_id' => $data['dynamic_id'],
            'user_id' => $data['user_id'],
            'content' => $data['content'],
            'status' => $data['status'],
            'create_time' => time(),
            'reply_user_id' => $data['reply_user_id'],
            'reply_comment_id' => $data['reply_comment_id'],
        ];
        $userInfo = UserModel::where('id', $data['user_id'])->where('type', 2)->field('id,ip,country,province,city')->find();
        if ($userInfo) {
            $model['ip'] = $userInfo['ip'];
            $model['country'] = $userInfo['country'];
            $model['province'] = $userInfo['province'];
            $model['city'] = $userInfo['city'];
        } else {
            $model = SuPengJun::ipStr($model, $request->ip());
        }
        return $this->model->insert($model);
    }

    public function update($id, $request)
    {
        $data = $this->preExcludeFields($request->param());
        $model = [
            'dynamic_id' => $data['dynamic_id'],
            'user_id' => $data['user_id'],
            'content' => $data['content'],
            'status' => $data['status'],
            'update_time' => time(),
            'reply_user_id' => $data['reply_user_id'],
            'reply_comment_id' => $data['reply_comment_id'],
        ];
        $userInfo = UserModel::where('id', $data['user_id'])->where('type', 2)->field('id,ip,country,province,city')->find();
        if ($userInfo) {
            $model['ip'] = $userInfo['ip'];
            $model['country'] = $userInfo['country'];
            $model['province'] = $userInfo['province'];
            $model['city'] = $userInfo['city'];
        } else {
            $model = SuPengJun::ipStr($model, $request->ip());
        }
        $this->model->where('id', $id)->update($model);
        return $id;
    }

    public function delete($id)
    {
        $comment = $this->model->where('reply_comment_id', $id)->count();
        if ($comment) {
            $this->model->where('id', $id)->update(['status' => 3]);
        } else {
            $this->model->where('id', $id)->delete();
        }
        return success('删除成功！');
    }
}
